﻿/*==========================================================================
	TASK 2:                                                  {IO Text Files}
	Write a program that concatenates two text files into another text file.
===========================================================================*/

using System;
using System.IO;

class ConcatenateTextFiles
{
    static void Main()
    {
        string inputFile1 = @"..\..\temp1.txt";//the dir of the .cs file
        string inputFile2 = @"..\..\temp2.txt";//the dir of the .cs file
        string outputFile = @"..\..\temp.txt";//the dir of the .cs file

        try
        {
            StreamReader readFile1 = new StreamReader(inputFile1);
            using (readFile1)
            {
                StreamReader readFile2 = new StreamReader(inputFile2);
                using (readFile2)
                {
                    StreamWriter writeFile3 = new StreamWriter(outputFile);
                    using (writeFile3)
                    {
                        string line = readFile1.ReadLine();

                        while (line != null)
                        {
                            writeFile3.WriteLine(line);
                            line = readFile1.ReadLine();
                        }

                        line = readFile2.ReadLine();

                        while (line != null)
                        {
                            writeFile3.WriteLine(line);
                            line = readFile2.ReadLine();
                        }
                    }
                }
            }
        }
        catch (FileNotFoundException e)
        {
            Console.Error.WriteLine(e.Message);
        }
        catch (DirectoryNotFoundException)
        {
            Console.Error.WriteLine("Invalid directory in the file path.");
        }
        catch (IOException e)
        {
            Console.Error.WriteLine(e.Message);
        }
    }
}

